package com.my.ruyi.schedule;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import com.my.ruyi.mapper.TriggerMapper;

@Component
public class QuartzSchedule {
	 @Autowired
	 private SchedulerFactoryBean schedulerFactoryBean;
	 
	 @Autowired
	 private TriggerMapper triggerMapper;

	 @PostConstruct
	 public void scheduleJobs() throws Exception {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		
		List<Map<String,Object>> list = triggerMapper.getTriggerAndJobDetailInfo();
		
		if (list != null && !list.isEmpty()) {
			String jobName = "",jobGroupName = "", jobClass = "",
					triggerName = "", triggerGroupName = "",
					triggerCron = "", httpHost = "",
					httpUrl = "", httpType = "";
			String newKey = "", breakKey = "";
			
			JobDetail jobDetail = null;
			
			for (Map<String, Object> map : list) {
				jobName = String.valueOf(map.get("jobName"));
				jobGroupName = map.get("jobGroupName") == null ? Scheduler.DEFAULT_GROUP : String.valueOf(map.get("jobGroupName"));
				jobClass = String.valueOf(map.get("jobClass"));
				triggerName = String.valueOf(map.get("triggerName"));
				triggerGroupName = map.get("triggerGroupName") == null ? Scheduler.DEFAULT_GROUP : String.valueOf(map.get("triggerGroupName"));
				triggerCron = String.valueOf(map.get("triggerCron"));
				httpHost = String.valueOf(map.get("httpHost"));
				httpUrl = String.valueOf(map.get("httpUrl"));
				httpType = String.valueOf(map.get("httpType"));
				
				newKey = jobGroupName + jobName;
				JobDataMap jobDataMap = new JobDataMap();
				jobDataMap.put("httpHost", httpHost);
				jobDataMap.put("httpUrl", httpUrl);
				jobDataMap.put("httpType", httpType);
				
				// 创建任务
				if (!breakKey.equals(newKey)) {
					jobDetail = JobBuilder.newJob(getClass(jobClass))
							.storeDurably(true) // 如果一个 Job 是不持久的， 一旦没有触发器与之关联，它就会被从 scheduler 中自动删除。
							//.setJobData(jobDataMap)
							.withDescription("demo")
							.withIdentity(jobName, jobGroupName).build();
				}
				
				// 表达式调度构建器
				CronScheduleBuilder schedBuilder = CronScheduleBuilder.cronSchedule(triggerCron);
				//CronScheduleBuilder.cronSchedule(CronExpression cronExpression)
				// 创建触发器
				// 在触发器中设置构建表达式
				CronTrigger trigger = TriggerBuilder.newTrigger()
						.forJob(jobDetail).usingJobData(jobDataMap)
						.withSchedule(schedBuilder)
						.withIdentity(triggerName, triggerGroupName)						
						.build();
				
				if (!breakKey.equals(newKey)) {
					scheduler.scheduleJob(jobDetail,trigger);
				}else {
					scheduler.scheduleJob(trigger);
				}
				
				breakKey = jobGroupName + jobName;
			}
			
			scheduler.start();
		}
		
		
	}
	 
	 public Class<? extends Job> getClass(String clzName) throws Exception {
		Class<?> forName = Class.forName(clzName);
		return (Class<? extends Job>) forName;
	}
}
